package com.gs.SynPatrolRep.control;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.gs.SynPatrolRep.R;

public class LabelChooseRadio extends LinearLayout {

    public interface OnItemCheckedChangeListener {
        void onItemCheckedChanged(int itemCheckedId);
    }

    private TextView item_lbl;
    private RadioButton item_radio1;
    private RadioButton item_radio2;
    private RadioGroup item_radioGp;

    public void setOnItemCheckedChangeListener(final OnItemCheckedChangeListener onItemCheckedChangeListener) {
        if (onItemCheckedChangeListener != null) {
            item_radioGp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    onItemCheckedChangeListener.onItemCheckedChanged(checkedId == item_radio1.getId() ? 0 : 1);
                }
            });
        }
    }

    private int valId;

    public String getLbl() {
        return item_lbl.getText().toString();
    }

    public void setLbl(String text) {
        item_lbl.setText(text);
    }

    public void setLblHint(String hint) {
        item_lbl.setHint(hint);
    }

    public int getValId() {
        return valId;
    }

    public void setValId(int valId) {
        this.valId = valId;
    }

    public Boolean getChecked() {
        if (item_radio1.isChecked()) {
            return true;
        } else if (item_radio2.isChecked()) {
            return false;
        }
        return true;
    }

    public void setChecked(Boolean checked) {
        if (checked == null) {
            item_radio1.setChecked(true);
        } else {
            if (checked) {
                item_radio1.setChecked(true);
            } else {
                item_radio2.setChecked(true);
            }
        }
    }

    public LabelChooseRadio(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.LabelChooseRadio);
        LayoutInflater.from(context).inflate(R.layout.label_choose_radio, this, true);
        item_lbl = findViewById(R.id.item_lbl);
        item_radioGp = findViewById(R.id.item_radioGp);
        item_radio1 = findViewById(R.id.item_radio1);
        item_radio2 = findViewById(R.id.item_radio2);
        item_radio1.setText("");
        item_radio2.setText("");
        item_lbl.setText("");
        String text = attributes.getString(R.styleable.LabelChooseRadio_label_choose_radio_radio1_text);
        String textHint = attributes.getString(R.styleable.LabelChooseRadio_label_choose_radio_radio1_hint);
        boolean isStar = attributes.getBoolean(R.styleable.LabelChooseRadio_label_choose_radio_star, false);
        item_radio1.setText(text);
        item_radio1.setHint(textHint);
        String textVisibility = attributes.getString(R.styleable.LabelChooseRadio_label_choose_radio_radio1_visibility);
        if (textVisibility != null && !TextUtils.isEmpty(textVisibility)) {
            switch (textVisibility) {
                case "visible":
                    item_radio1.setVisibility(VISIBLE);
                    break;
                case "invisible":
                    item_radio1.setVisibility(INVISIBLE);
                    break;
                case "gone":
                    item_radio1.setVisibility(GONE);
                    break;
            }
        }
        text = attributes.getString(R.styleable.LabelChooseRadio_label_choose_radio_radio2_text);
        textHint = attributes.getString(R.styleable.LabelChooseRadio_label_choose_radio_radio2_hint);
        item_radio2.setText(text);
        item_radio2.setHint(textHint);
        textVisibility = attributes.getString(R.styleable.LabelChooseRadio_label_choose_radio_radio2_visibility);
        if (textVisibility != null && !TextUtils.isEmpty(textVisibility)) {
            switch (textVisibility) {
                case "visible":
                    item_radio2.setVisibility(VISIBLE);
                    break;
                case "invisible":
                    item_radio2.setVisibility(INVISIBLE);
                    break;
                case "gone":
                    item_radio2.setVisibility(GONE);
                    break;
            }
        }

        String lbl = attributes.getString(R.styleable.LabelChooseRadio_label_choose_radio_lbl);
        if (isStar) {
            Spannable spannable = new SpannableString("* " + lbl);
            spannable.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.deeper_red)), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            item_lbl.setText(spannable);
        } else {
            item_lbl.setText(lbl);
        }
        String lblVisibility = attributes.getString(R.styleable.LabelChooseRadio_label_choose_radio_lbl_visibility);
        if (lblVisibility != null && !TextUtils.isEmpty(lblVisibility)) {
            switch (lblVisibility) {
                case "visible":
                    item_lbl.setVisibility(VISIBLE);
                    break;
                case "invisible":
                    item_lbl.setVisibility(INVISIBLE);
                    break;
                case "gone":
                    item_lbl.setVisibility(GONE);
                    break;
            }
        }
        attributes.recycle();
    }
}
